package mx.com.mindbits.bandpay.fragments;

import java.text.SimpleDateFormat;

import mx.com.mindbits.bandpay.R;
import mx.com.mindbits.bandpay.TransactionDetailActivity;
import mx.com.mindbits.bandpay.dao.BandPayDAO;
import mx.com.mindbits.bandpay.utils.ConstantsValues;
import mx.com.mindbits.bandpay.utils.Utils;
import mx.com.mindbits.dao.transactions;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockFragment;

public class TransactionDetailFragment extends SherlockFragment{
	
	private long idTransaction = -1;
	private Context context = null;
	private BandPayDAO bAndPayDAO;
	private TextView tvDescripcion; 
	private TextView tvCompania;
	private TextView tvMonto;
	private TextView tvFecha;
	private TextView tvEstado;
	private TextView tvNumero;

	public void setIdTransaction(long idTransaction){
		this.idTransaction = idTransaction;
	}
	
	public long getIdTransaction(){
		return idTransaction;
	}

	public Context getContext() {
		return context;
	}

	public void setContext(Context context) {
		this.context = context;
	}

	public TransactionDetailFragment(){
		
	}
	
	public TransactionDetailFragment(long idTransaction, Context context) {
		this.idTransaction = idTransaction;
		this.context = context;
	}
	
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		TransactionDetailActivity transDetailActivity = (TransactionDetailActivity) activity; 
		idTransaction = transDetailActivity.getIdRx();
		context = transDetailActivity.getBaseContext();
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
		View view = inflater.inflate(R.layout.fragment_detail, container, true);
		populateLayoutWithData(view);
		return view;
	}
	
	private void populateLayoutWithData(View view){
		Log.d(ConstantsValues.TAG_TRANS_DETAIL_HISTORY, "Entrando a populateLayoutWithData");
		Log.d(ConstantsValues.TAG_TRANS_DETAIL_HISTORY, "idTransaction: " + idTransaction);
		if(idTransaction != -1 && context != null){
			Log.d(ConstantsValues.TAG_TRANS_DETAIL_HISTORY, 
					"IdTransaction es diferente de -1 y context diferente de nulo");
			
			bAndPayDAO = new BandPayDAO(context);
			Resources resources = getResources();
			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(ConstantsValues.FORMATO_FECHA_ES);
						
			tvDescripcion = (TextView) view.findViewById(R.id.id_tv_descripcion);
			tvCompania = (TextView) view.findViewById(R.id.id_tv_compania);
			tvMonto = (TextView) view.findViewById(R.id.id_tv_monto);
			tvFecha = (TextView) view.findViewById(R.id.id_tv_fecha);
			tvEstado = (TextView) view.findViewById(R.id.id_tv_estado);
			tvNumero = (TextView) view.findViewById(R.id.id_tv_numeroTAE);
			
			transactions transaction = bAndPayDAO.getTransactionById(idTransaction);
			
			Log.d(ConstantsValues.TAG_TRANS_DETAIL_HISTORY, "Transaction: " + transaction);
			
			if(transaction != null){
				tvDescripcion.setText(tvDescripcion.getText() + ": " + transaction.getDescripcion());
				tvCompania.setText(tvCompania.getText() + ": " + 
				Utils.getCompanyLabel(String.valueOf(transaction.getProveedor())));
				tvMonto.setText(tvMonto.getText() + " " + String.valueOf(transaction.getMonto()));
				tvFecha.setText(tvFecha.getText() + ":" + simpleDateFormat.format(transaction.getFecha()) +
						" " + transaction.getHora());
				if(transaction.getNumeroTae() == 0)
					tvNumero.setVisibility(TextView.INVISIBLE);
				else
					tvNumero.setText(tvNumero.getText() + ": "+transaction.getNumeroTae());
				
				int idEstadoString = (transaction.getEstatus() == ConstantsValues.STATUS_PROCESADO) ?
						R.string.estatus_g : R.string.estatus_b;
				int styleId = (transaction.getEstatus() == ConstantsValues.STATUS_TRANS_EFEC) ? 
						R.style.Text_TransEfectuada : R.style.Text_TransNoEfectuada;
				tvEstado.setText(resources.getString(idEstadoString) + " ");
				//tvEstado.setText("" + transaction.getEstatus());
				tvEstado.setTextAppearance(context, styleId);
			}
			
		}
		Log.d(ConstantsValues.TAG_TRANS_DETAIL_HISTORY, "Saliendo de populateLayoutWithData");
	}
	
	
}
